import http.server
import socketserver
import argparse
import os

class CustomHandler(http.server.SimpleHTTPRequestHandler):
    def translate_path(self, path):
        # 获取请求的实际路径
        root = os.getcwd() + ROOT_DIR
        print("root", root)
        return os.path.join(root, path.lstrip('/'))

# 将目录作为参数进行传递
parser = argparse.ArgumentParser()
parser.add_argument("--dir", help="指定网站根目录")
args = parser.parse_args()

if args.dir:
    ROOT_DIR = args.dir
else:
    ROOT_DIR = ""

PORT = 8000

with socketserver.TCPServer(("0.0.0.0", PORT), CustomHandler) as httpd: 
    print("Server running at port", PORT, ROOT_DIR)
    httpd.serve_forever()

